package com.wl156.activities.find;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.MyApp;
import com.wl156.lite.R;
import com.wl156.activities.Xinyongxingji;
import com.wl156.activities.order.NewOrder;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.model.Wl156DB;
import com.wl156.utils.Utils;

public class CarMessageInfo extends Activity implements OnClickListener {

	TextView mCarModel;
	TextView mCarWeight;
	TextView mCarLength;
	TextView mContact;
	TextView mPhone;
	TextView mNeed;
	TextView mOfenRunLine;
	TextView mDateShow;
	private TextView mProvice;
	private TextView mCity;
	private TextView mRegion;
	private TextView mToProvice;
	private TextView mToCity;
	private TextView mToRegion;
	private TextView mComment;
	private TextView mCurrentPlace;
	private TextView mSetOffDate;
	private TextView mXinyongScore;
	private Button mXinyongDetail;

	private ImageButton mCall;
	private Button mCollect;

	private long mCarId;
	private String userID;

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	boolean fromMy = false;
	private Button mYichengjiao;
	
	boolean mIsOrderSelect;
	Button mOrderSelect;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_car_message_info);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mCarId = getIntent().getLongExtra("id", 0);
		fromMy = getIntent().getBooleanExtra("frommy", false);
		mYichengjiao = (Button) findViewById(R.id.chengjiao);
		mYichengjiao.setOnClickListener(this);
		if (!fromMy) {
			mYichengjiao.setVisibility(View.GONE);
		}

		mProvice = (TextView) findViewById(R.id.provice);
		mToProvice = (TextView) findViewById(R.id.to_provice);
		mComment = (TextView) findViewById(R.id.comment);
		mCarModel = (TextView) findViewById(R.id.car_model);
		mCarWeight = (TextView) findViewById(R.id.weight);
		mCarLength = (TextView) findViewById(R.id.length);
		mContact = (TextView) findViewById(R.id.contact);
		mPhone = (TextView) findViewById(R.id.phone);
		mNeed = (TextView) findViewById(R.id.need);
		mOfenRunLine = (TextView) findViewById(R.id.run_roads);
		mXinyongScore = (TextView) findViewById(R.id.xinyongscore);
		mXinyongDetail = (Button) findViewById(R.id.xinyongdetail);
		mXinyongDetail.setOnClickListener(this);

		mCurrentPlace = (TextView) findViewById(R.id.current_place);
		mSetOffDate = (TextView) findViewById(R.id.set_off_date);

		mCall = (ImageButton) findViewById(R.id.call);
		mCall.setEnabled(false);
		mCall.setOnClickListener(this);
		mCollect = (Button) findViewById(R.id.collect);
		mCollect.setOnClickListener(this);
		
		mOrderSelect = (Button) findViewById(R.id.order_select);
		mOrderSelect.setOnClickListener(this);
		
		mIsOrderSelect = getIntent().getBooleanExtra("order_select", false);
		if (mIsOrderSelect) {
			mOrderSelect.setVisibility(View.VISIBLE);
		} else {
			mOrderSelect.setVisibility(View.GONE);
		}

		new AsyncTask<Void, Void, Object>() {
			ProgressDialog mDialog;

			@Override
			protected void onPostExecute(Object result) {
				// 02-25 23:02:27.062: D/(26728): result: anyType{string=111;
				// string=广东广州白云区; string=湖南; string=长板车; string=10; string=8;
				// string=2012-3-1; string=张超人; string=13545678912;
				// string=66912; string=不运危险品（如花炮）; string=广东省广州市白云区 2月22日14时;
				// string=广东广州|云南昆明;广东广州|河南;; string=12; string=2; }, type:
				// SoapObject

				try {
					if (result instanceof SoapObject) {
						SoapObject so = (SoapObject) result;
						String id = Utils.propertyToShow(so.getProperty(0));
						String start = Utils.propertyToShow(so.getProperty(1));
						String end = Utils.propertyToShow(so.getProperty(2));
						String type = Utils.propertyToShow(so.getProperty(3));
						String weight = Utils.propertyToShow(so.getProperty(4));
						String length = Utils.propertyToShow(so.getProperty(5));
						String setOffDate = Utils.propertyToShow(so
								.getProperty(6));
						String contact = Utils
								.propertyToShow(so.getProperty(7));
						String phone = Utils.propertyToShow(so.getProperty(8));
						String internalPhone = Utils.propertyToShow(so
								.getProperty(9));
						String need = Utils.propertyToShow(so.getProperty(10));
						String currentPlace = Utils.propertyToShow(so
								.getProperty(11));
						String oftenLine = Utils.propertyToShow(so
								.getProperty(12));
						String creditScore = Utils.propertyToShow(so
								.getProperty(13));
						String finishOrderCount = Utils.propertyToShow(so
								.getProperty(14));
						userID = Utils.propertyToShow(so.getProperty(18));
						
						MyApp.sCarInfo.id = id;
//						MyApp.sCarInfo.driverName = driverName;
//						MyApp.sCarInfo.driverPhone = driverPhone;
//						MyApp.sCarInfo.driverPhoneShort = driverPhoneShort;
//						MyApp.sCarInfo.ownerName = ownerName;
//						MyApp.sCarInfo.ownerPhone = ownerPhone;
//						MyApp.sCarInfo.carCode = carCode;
//						MyApp.sCarInfo.carType = carType;
//						MyApp.sCarInfo.carWeigth = carWeigth;
//						MyApp.sCarInfo.carLength = carLength;
//						MyApp.sCarInfo.need = need;
//						MyApp.sCarInfo.currentPlace = currentPlace;
//						MyApp.sCarInfo.oftenLine = oftenLine;
//						MyApp.sCarInfo.creditScore = creditScore;
//						MyApp.sCarInfo.finishOrderCount = finishOrderCount;

						mProvice.setText(start);
						mToProvice.setText(end);
						mCarModel.setText(type);
						mCarWeight.setText(weight);
						mCarLength.setText(length);
						mContact.setText(contact);
						mPhone.setText(phone);
						mNeed.setText(need);
						mOfenRunLine.setText(Utils.anyTypeToEmpty(oftenLine));
						mCurrentPlace.setText(Utils
								.anyTypeToEmpty(currentPlace));
						mSetOffDate.setText(Utils.anyTypeToEmpty(setOffDate));
						mXinyongScore.setText(creditScore);

						mCall.setEnabled(true);
					} else {
						Toast.makeText(CarMessageInfo.this, "获取信息失败，请稍候再试",
								Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					Toast.makeText(CarMessageInfo.this, "获取信息失败，请稍候再试",
							Toast.LENGTH_LONG).show();
				}
				mDialog.dismiss();
			};

			@Override
			protected void onPreExecute() {
				mDialog = ProgressDialog.show(CarMessageInfo.this, "", "加载中");
			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.commonFindTruckMessageDetail(
							mCertificate, mCarId);
				} catch (IOException e) {
					return e;
				} catch (XmlPullParserException e) {
					return e;
				}
			}
		}.execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.order_select: {
			Intent i = new Intent(this, NewOrder.class);
			startActivity(i);
		}
			break;
		case R.id.xinyongdetail: {
			Intent i = new Intent(this, Xinyongxingji.class);
			i.putExtra("userID", userID);
			startActivity(i);
		}
			break;
		case R.id.chengjiao: {
			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(CarMessageInfo.this, "",
							"加载中");
				};

				@Override
				protected void onPostExecute(Object result) {
					mDialog.dismiss();
					Toast.makeText(CarMessageInfo.this, "成功", Toast.LENGTH_LONG)
							.show();
				}

				@Override
				protected Object doInBackground(Void... params) {
					try {
						Wl156DB.changeCarOrGoodFinish(CarMessageInfo.this, "1",
								String.valueOf(mCarId));
						return mWebServiceClient.driver_CarInfo(mCertificate,
								String.valueOf(mCarId));
					} catch (IOException e) {
						e.printStackTrace();
						return e;
					} catch (XmlPullParserException e) {
						e.printStackTrace();
						return e;
					}
				}
			}.execute();
		}
			break;
		case R.id.call:
			Intent i = new Intent(Intent.ACTION_DIAL);
			i.setData(Uri.parse("tel:" + mPhone.getText().toString()));
			startActivity(i);
			break;
		case R.id.collect:
			break;
		}
	}
}
